//
//  YKPopoverView.m
//  YKKit
//
//  Created by 马洪伟 on 16/1/13.
//  Copyright © 2016年 悦康动力（北京）科技有限公司. All rights reserved.
//

#import "YKPopoverView.h"

@interface YKPopoverView()
{
    YKPopoverViewAnimation _showAnimation;
}
@end

@implementation YKPopoverView

- (instancetype)init {
    return [self initWithContentView:nil];
}
- (YKPopoverView *)initWithContentView:(UIView *)contentView {
    return [self initWithFrame:[UIScreen mainScreen].bounds contentView:contentView];
}
- (YKPopoverView *)initWithFrame:(CGRect)frame contentView:(UIView *)contentView {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.contentView = contentView;
        [self addTarget:self action:@selector(didClickBackground) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)showInView:(UIView *)view animation:(YKPopoverViewAnimation)animation {
    _showAnimation = animation;
    switch (animation) {
        case YKPopoverViewAnimationNone:
        {
            [self showInViewWithoutAnimation:view];
        }
            break;
        case YKPopoverViewAnimationPresent:
        {
            [self presentInView:view];
        }
            break;
        case YKPopoverViewAnimationAlert:
        {
            [self alertInView:view];
        }
            break;
        case YKPopoverViewAnimationSlideDown:
        {
            [self slidedownInView:view];
        }
            break;
            
        default:
        {
            [self showInViewWithoutAnimation:view];
        }
            break;
    }
}

- (void)dismiss:(BOOL)animation {
    [[NSNotificationCenter defaultCenter] postNotificationName:YKPopoverViewDismissNotification object:self];
    
    if (animation) {
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            if (_showAnimation == YKPopoverViewAnimationSlideDown) {
                self.contentView.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, 0);
            }
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            if (_hideBlock) {
                _hideBlock();
            }
        }];
    } else {
        [self removeFromSuperview];
    }
}

- (void)showInViewWithoutAnimation:(UIView *)superView {
    [superView addSubview:self];
}
- (void)presentInView:(UIView *)superView {
    CGRect destination = self.contentView.frame;
    self.contentView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width, 0);
    [superView addSubview:self];
    [UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations: ^{
                         self.contentView.frame = destination;
                         self.alpha = 1.0f;
                     } completion: ^(BOOL finished) {
                     }];
}
- (void)alertInView:(UIView *)superView {
    [superView addSubview:self];
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    CATransform3D scale1 = CATransform3DMakeScale(0.5, 0.5, 1);
    CATransform3D scale2 = CATransform3DMakeScale(1.2, 1.2, 1);
    CATransform3D scale3 = CATransform3DMakeScale(0.9, 0.9, 1);
    CATransform3D scale4 = CATransform3DMakeScale(1.0, 1.0, 1);
    
    NSArray *frameValues = [NSArray arrayWithObjects:
                            [NSValue valueWithCATransform3D:scale1],
                            [NSValue valueWithCATransform3D:scale2],
                            [NSValue valueWithCATransform3D:scale3],
                            [NSValue valueWithCATransform3D:scale4],
                            nil];
    [animation setValues:frameValues];
    
    NSArray *frameTimes = [NSArray arrayWithObjects:
                           [NSNumber numberWithFloat:0.0],
                           [NSNumber numberWithFloat:0.5],
                           [NSNumber numberWithFloat:0.9],
                           [NSNumber numberWithFloat:1.0],
                           nil];
    [animation setKeyTimes:frameTimes];
    
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.duration = .2;
    
    [self.contentView.layer addAnimation:animation forKey:@"popup"];
}
- (void)slidedownInView:(UIView *)superView {
    CGRect destination = self.contentView.frame;
    self.contentView.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, 0);
    
    [superView addSubview:self];
    
    [UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations: ^{
                         self.contentView.frame = destination;
                         self.alpha = 1.0f;
                     } completion: ^(BOOL finished) {
                     }];
}

- (void)setContentView:(UIView *)contentView {
    if (contentView) {
        if (_contentView) {
            [_contentView removeFromSuperview];
        }
        _contentView = contentView;
        [self addSubview:_contentView];
    }
}

- (void)didClickBackground {
    if (!_disableBackgroundTouch) {
        [self dismiss:YES];
    }
}
@end

NSString *YKPopoverViewDismissNotification = @"YKPopoverViewDismissNotification";
